package com.appointmentmanager.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.appointmentmanager.dao.UserDao;
import com.appointmentmanager.model.Project;
import com.appointmentmanager.model.User;
import com.appointmentmanager.service.ProjectService;
import com.appointmentmanager.service.UserService;

@Controller
@RequestMapping("/admin/users")
public class ManageUserController {
//	private Logger logger = LoggerFactory.getLogger(getClass()); 

	private UserDao userDao;
	private UserService userService;
	private ProjectService projectService;
//	private ResourceService resourceService;
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	
//	public void setResourceService(ResourceService resourceService) {
//		this.resourceService = resourceService;
//	}
	
	@RequestMapping(method = RequestMethod.GET)
    public String displayUsers(Model model) {
		model.addAttribute("users",	this.userDao.getUsers());
		
//		logger.debug(userService.getCurrentUser().toString());
		
		model.addAttribute("currentUser", userService.getCurrentUser());
		
		return "manageuser";
	}
	/*	
	// response to 'manageuser.jsp'
	@RequestMapping(value = "/{uid}/projects", method = RequestMethod.GET)
	public String viewProject(@PathVariable Long uid, Model model) {
		User user = userService.findUser(uid);
		if (user == null) {
			return "redirect:/error/400.jsp";
		}
		
		model.addAttribute("currentUser", userService.getCurrentUser());
		model.addAttribute("user", user);
		model.addAttribute("projectList", projectService.findManagedProjects(user));
		
		return "userproject";
	}
		
	@RequestMapping(value = "/{uid}/{pid}/resources", method = RequestMethod.GET)
	public String viewResource(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid, Model model) {
		logger.debug("i am here");
		
		User user = userService.findUser(uid);
		if (user == null) {
			logger.debug("i am here " + user);
			return "redirect:/error/400.jsp";
		}
		
		Project project = this.projectService.findProject(pid);
		if (project == null) {
			logger.debug("i am here " + user);
			
			return "redirect:/error/400.jsp";
		}
		
	List<Resource> resourceList = resourceService.findProjectResourcesByUserId(uid, pid);
		List<Resource> currentResource = new ArrayList<Resource>();
		if (resourceList != null) {
			for (Resource r : resourceList) {
				logger.debug("getFid" + r.getFid());
				if (r.getFid() <= 0) {
					currentResource.add(r);
				}
			}
		}
		
		model.addAttribute("currentUser", userService.getCurrentUser());
		model.addAttribute("user", user);
		model.addAttribute("project", project);
		model.addAttribute("resources", currentResource);
		model.addAttribute("folders", resourceService.findSubfoldersByPid(pid));
		
		return "userresource";
	}*/
	
	/*
	@RequestMapping(value = "/{uid}/{pid}/{fid}/resources", method = RequestMethod.GET)
	public String viewResource(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid, 
			@PathVariable("fid") Long fid, Model model) {
		User user = userService.findUser(uid);
		if (user == null) {
			return "redirect:/error/400.jsp";
		}
		
		Project project = this.projectService.findProject(pid);
		if (project == null) {
			return "redirect:/error/400.jsp";
		}
		
		Folder folder = resourceService.findFolder(fid);
		if (folder == null) {
			return "redirect:/error/400.jsp";
		}
		
		List<Resource> resourceList = resourceService.findProjectResourcesByUserId(uid, pid);
		List<Resource> currentResource = new ArrayList<Resource>();
		if (resourceList != null) {
			for (Resource r : resourceList) {
				logger.debug("getFid" + r.getFid());
				if (r.getFid() == fid) {
					currentResource.add(r);
				}
			}
		}
		
		List<Folder> traceFolders = resourceService.findPreFoldersById(fid);
		
		model.addAttribute("currentUser", userService.getCurrentUser());
		model.addAttribute("user", user);
		model.addAttribute("project", project);
		/*model.addAttribute("folder", folder);
		model.addAttribute("resources", currentResource);
		model.addAttribute("folders", resourceService.findSubfolders(fid));
		model.addAttribute("traceFolders", traceFolders);
		
		return "userresource";
	}*/
	
	@RequestMapping(value = "/{uid}/enable", method = RequestMethod.GET)
	public String enableUser(@PathVariable Long uid) {
		User user = userService.findUser(uid);
		if (user == null) {
			return "redirect:/error/400.jsp";
		}
		
		this.userDao.setEnabled(uid, true);
		return "redirect:/admin/users";
	}
	
	@RequestMapping(value = "/{uid}/disable", method = RequestMethod.GET)
	public String disableUser(@PathVariable Long uid) {
		User user = userService.findUser(uid);
		if (user == null) {
			return "redirect:/error/400.jsp";
		}
		
		this.userDao.setEnabled(uid, false);
		return "redirect:/admin/users";
	}
	/*
	// response to 'userproject.jsp'
	@RequestMapping(value = "/{uid}/projects/remove", method = RequestMethod.GET)
	public String deleteProject(@RequestParam Long pid, @PathVariable Long uid) {
		User user = userService.findUser(uid);
		if (user == null) {
			return "redirect:/error/400.jsp";
		}
		
		this.projectService.deleteProject(pid);
		return "redirect:/admin/users/" + uid + "/projects";
	}
	
	// response to 'userresource.jsp'
	@RequestMapping(value = "/{uid}/{pid}/resources/delete", method = RequestMethod.GET)
	public String deleteResource(@RequestParam Long rid, @PathVariable("uid") Long uid, 
			@PathVariable("pid") Long pid) {
		// TODO: rid name
		
		logger.debug("deleteResource");
		
		User user = userService.findUser(uid);
		if (user == null) {
			return "redirect:/error/400.jsp";
		}
		
		Resource resource = this.resourceService.findResource(rid);
		if (resource == null) {
			return "redirect:/error/400.jsp";
		}
		
		this.resourceService.removeResource(resource);
			
		return "redirect:/admin/users/" + uid + "/" + pid + "/resources";
	}
	
	// response to 'userresource.jsp'
		@RequestMapping(value = "/{uid}/{pid}/{fid}/resources/delete", method = RequestMethod.GET)
		public String deleteFolderResource(@RequestParam Long rid, @PathVariable("uid") Long uid, 
				@PathVariable("pid") Long pid, @PathVariable("fid") Long fid) {
			// TODO: rid name
			
			logger.debug("deleteResource");
			
			User user = userService.findUser(uid);
			if (user == null) {
				return "redirect:/error/400.jsp";
			}
			
			Resource resource = this.resourceService.findResource(rid);
			if (resource == null) {
				return "redirect:/error/400.jsp";
			}
			
			this.resourceService.removeResource(resource);
				
			return "redirect:/admin/users/" + uid + "/" + pid + "/" + fid + "/resources";
		}
	
	@RequestMapping(value = "/{uid}/resources/download", method = RequestMethod.GET)
	public void processDownloadProjectResources(@PathVariable("uid") Long uid,
			@RequestParam(value = "rid", required = true) Long rid,
			HttpServletResponse response) throws IOException {

		logger.debug("download resource");
		logger.debug("the uid " + uid);
		logger.debug("the rid " + rid);

		User user = this.userService.findUser(uid);
		if (user == null) {
			response.setStatus(400);
			return;
		}
		
		Resource resource = this.resourceService.findResource(rid);
		if (resource == null) {
			response.setStatus(400);
			return;
		}
		
		response.setContentType(resource.getType());
		response.setContentLength(resource.getFile().length);
		response.setHeader("Content-Disposition","attachment; filename=\""
        						+ resource.getName() +"\"");
		FileCopyUtils.copy(resource.getFile(), response.getOutputStream());
	}*/
}
